import { hasAnchor as f } from './identity.js';
function s(o, t, r) {
  if (Array.isArray(o)) return o.map((n, e) => s(n, String(e), r));
  if (o && typeof o.toJSON == 'function') {
    if (!r || !f(o)) return o.toJSON(t, r);
    const n = { aliasCount: 0, count: 1, res: void 0 };
    r.anchors.set(o, n),
      (r.onCreate = i => {
        (n.res = i), delete r.onCreate;
      });
    const e = o.toJSON(t, r);
    return r.onCreate && r.onCreate(e), e;
  }
  return typeof o == 'bigint' && !(r != null && r.keep) ? Number(o) : o;
}
export { s as toJS };
